﻿using System.Net.Sockets;
using System.Windows.Media.Media3D;
using ModuleBase;
using NModbus;
using Prism.Ioc;
using Prism.Modularity;

namespace CameraCon
{
    public class CameraCon : ModuleBase.ModuleBase
    {
        private IContainerProvider _containerProvider;
        private IHIKCameraService camera;
        public bool Connected { get; set; }
        bool outputs = false;
        bool isExecuted = false;
        private string _parameter1;
        public string Parameter1
        {
            get => _parameter1;
            set
            {
                if (_parameter1 != value)
                {
                    _parameter1 = value;
                    OnPropertyChanged(nameof(Parameter1));
                }
            }
        }
        private string _parameter2 = "连接";
        public string Parameter2
        {
            get => _parameter2;
            set
            {
                if (_parameter2 != value)
                {
                    _parameter2 = value;
                    OnPropertyChanged(nameof(Parameter2));
                }
            }
        }
        private string _parameter3 = "Camera1";
        public string Parameter3
        {
            get => _parameter3;
            set
            {
                if (_parameter3 != value)
                {
                    _parameter3 = value;
                    OnPropertyChanged(nameof(Parameter3));
                }
            }
        }
        public override Dictionary<string, object> GetProperties()
        {
            return new Dictionary<string, object>
            {
                { "IP", Parameter1 },
                { "操作", Parameter2 },
                { "相机", Parameter3 },
            };
        }
        public override Dictionary<string, List<object>> PropertyOptions { get; } = new Dictionary<string, List<object>>
        {
            { "操作" ,new List<object>{ "连接","断开" } },
            { "相机" ,new List<object>{ "Camera1", "Camera2", "Camera3", "Camera4" } },
        };
        public CameraCon(IContainerProvider containerProvider)
        {
            _containerProvider = containerProvider;
            Name = "相机连接";

            DisplayNameToPropertyNameMap = new Dictionary<string, string>
            {
                { "IP", nameof(Parameter1) },
                { "操作", nameof(Parameter2) },
                { "相机", nameof(Parameter3) },
            };
        }
        public override bool Execute()
        {
            // 动态注册（如果尚未注册）
            var containerExtension = _containerProvider.Resolve<IContainerExtension>();
            if (!_containerProvider.IsRegistered<IHIKCameraService>(Parameter3))
            {
                containerExtension.RegisterSingleton<IHIKCameraService, HIKCameraService>(Parameter3);
            }
            camera = _containerProvider.Resolve<IHIKCameraService>(Parameter3);

            Dictionary<string, object> keyValuePairs = new Dictionary<string, object>();
            Status = ModuleStatus.Executing; // 设置状态为执行中

            if (isExecuted)
            {
                outputs = true;
                return outputs;
            }
            try
            {
                if (Parameter2 == "连接")
                {
                    var r = camera.OpenCamera(Parameter1);
                    if (r)
                    {
                        outputs = true;
                        isExecuted = true;
                    }
                    else
                    {
                        outputs = false;
                        isExecuted = false;
                    }
                    MyFunc.AddMessage($"相机{Parameter3} [{Parameter1}] 连接：{r} ");
                }
                else
                {
                    camera.CloseCamera();
                    outputs = true;
                    isExecuted = false;
                }
            }
            catch (Exception ex)
            {
                MyFunc.AddMessage($"相机 {Parameter3}{Parameter1} 连接异常：{ex.ToString()} ");
                outputs = false;
            }

            return outputs;
        }

        public override async Task<bool> ExecuteAsync()
        {

            outputs = false;
            var result = await Task.Run(() => Execute());
            return result;
        }

        public override void Initialize()
        {
            isExecuted = false;
        }

        public override string Description => "相机连接模块：用于连接或断开指定工业相机，支持多相机管理。";
    }
}
